package com.zhongxi.guoyuan.fragment.beautifulStore;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.ImageDetailAdapter;
import com.zhongxi.guoyuan.adapter.beautifulStore.DetailStepAdapter;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.projectItem.ProjectDetailInfo;
import com.zhongxi.guoyuan.entity.projectItem.ProjectDetailResult;
import com.zhongxi.guoyuan.widget.CircleIndicator;
import com.zhongxi.guoyuan.widget.ViewFlow;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */

@SuppressLint("ValidFragment")
public class BeautifulStoreDetailFragment extends BaseFragment {

    private ViewFlow mViewFlow;
    private ImageDetailAdapter mDetailAdapter;
    private NoScrollListView lvList;
    private List<String> data = new ArrayList<String>();
    private DetailStepAdapter adapter;
    private ImageView ivCall;
    private String id;
    private WebView wv;
    private String type;

    public BeautifulStoreDetailFragment(String id) {
        super();
        this.id = id;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        wv = (WebView) getView().findViewById(R.id.wv);
        initWebView();
    }

    private void initWebView() {
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setBuiltInZoomControls(false);
        wv.getSettings().setDisplayZoomControls(false);
        wv.getSettings().setDefaultTextEncodingName("UTF-8");
        wv.getSettings().setBlockNetworkImage(false);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            wv.getSettings().setMixedContentMode(wv.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    private void lunbo(List<ProjectDetailInfo.ProjectData> description) {


        //初始化轮播控件
        mViewFlow = (ViewFlow) getView().findViewById(R.id.viewflow);
        CircleIndicator mViewIndic = (CircleIndicator) getView().findViewById(R.id.viewflowdic);
        mViewFlow.setFlowIndicator(mViewIndic);

        if(description.size()>0)
            mViewFlow.setSideBuffer(description.size()); // 实际图片数量
        if(mDetailAdapter == null){
            mDetailAdapter = new ImageDetailAdapter(context, description);
            mViewFlow.setAdapter(mDetailAdapter);
        }else{
            mDetailAdapter.replaceAll(description);
        }
        mViewFlow.startAutoFlowTimer();
    }

    private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        return doc.toString();
    }

    public void initProjectData(ProjectDetailResult result) {
        try{
            if(null!=result.getExt()&&!"".equals(result.getExt())){
                ProjectDetailInfo ext = result.getExt();

                if(null!=ext.getAccessories()&&!"".equals(ext.getAccessories())){
                    if(ext.getAccessories().size()>0)
                    lunbo(ext.getAccessories());
                }
                if(null!=result.getExt().getDescription()&&!"".equals(result.getExt().getDescription())){
                    wv.loadDataWithBaseURL(null, getNewContent(result.getExt().getDescription()), "text/html", "utf-8", null);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
